import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { RouteRecordRaw } from 'vue-router'

type MessageTypes = 'success' | 'info' | 'warning' | 'error'
type MessageTypesName = '成功' | '提示' | '警告' | '错误'

export type Confirm = (massage: string, callback: () => void, type?: MessageTypes, title?: string) => void
export type ConfirmForm = (
	massage: string | any,
	callback: (value: string) => void,
	type?: MessageTypes,
	title?: string,
	formConfig?: { inputPattern?: RegExp; inputErrorMessage: string },
	dangerouslyUseHTMLString?: boolean
) => void
export type Notification = (message: string, title?: MessageTypesName, type?: MessageTypes) => void
export type Message = (message: string, type?: MessageTypes) => void
export type FilterRoutes = (routes: RouteRecordRaw[]) => Array<RouteRecordRaw>

export interface LoginCanvasType {
	clear: () => void
	startAnima: () => void
	closeAnima: () => void
}

export type ModuleTypes = {
	filterRoutes: FilterRoutes
	confirm: Confirm
	confirmForm: ConfirmForm
	message: Message
	notification: Notification
}

export const confirm: Confirm = (message, callback, type = 'info', title = '提示') => {
	ElMessageBox.confirm(message, title, {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: type
	})
		.then(() => {
			callback()
		})
		.catch(() => ({}))
}

export const confirmForm: ConfirmForm = (
	message = '',
	callback,
	type,
	title = '提交',
	formConfig = {
		inputPattern: /.+/,
		inputErrorMessage: '格式错误'
	}
) => {
	ElMessageBox.prompt(message, title, {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: type,
		...formConfig
	})
		.then(({ value }: { value: string }) => {
			callback(value)
		})
		.catch(() => {
			console.log('-------------- cancel: ')
		})
}

export const message: Message = (message, type = 'error') => {
	ElMessage({
		message,
		type
	})
}

export const notification: Notification = (message, title = '提示', type = 'info') => {
	ElNotification({
		title,
		message,
		type
	})
}
